package com.itcam.interview.pro01javase;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class Demo05BigDecimal {
    /*
        面试官：看你简历上写了电商项目做过支付/购物车模块，一定涉及过金额处理的操作吧？请问你用哪些类型？精度如何处理的？有没有碰到过坑？
     */

    public static void main(String[] args) {
//        double amount1 = 0.3;
//        double amount2 = 0.2;
//        System.out.println(amount1 - amount2);

//        BigDecimal amount1 = new BigDecimal(0.3);
        BigDecimal amount1 = new BigDecimal("0.3");
////        System.out.println("amount1 = " + amount1);
        BigDecimal amount2 = new BigDecimal("0.30");
//        System.out.println(amount1.subtract(amount2));

//        BigDecimal amount1 = BigDecimal.valueOf(0.3);
//        BigDecimal amount2 = BigDecimal.valueOf(0.30);

        // 比值 equals比较的话new出来0.6和0.60的会是false
        System.out.println(amount1.equals(amount2));
        System.out.println(amount1.compareTo(amount2));

        BigDecimal number1 = new BigDecimal("2.0");
        BigDecimal number2 = new BigDecimal("3.0");
//        System.out.println(number1.divide(number2)); // Non-terminating decimal expansion; no exact representable decimal result.
        System.out.println(number1.divide(number2,2, RoundingMode.HALF_UP));


        // 科学计数法

    }
}
